<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="校验名:" prop="checkName">
            <el-input readOnly="true" v-model="formModel.checkName"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校验代码:" prop="checkNo">
            <el-input readOnly="true" v-model="formModel.checkNo"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校验范围:" prop="checkRange">
            <el-input  v-model="formModel.checkRange"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="正则表达式:" prop="regex">
            <el-input  v-model="formModel.regex"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="长度:" prop="length">
            <el-input v-model="formModel.length"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'TmBasMaterialRelationMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      formModel: {
        checkRuleConfigId: '',
        checkName: '',
        checkNo: '',
        checkRange: '',
        regex: '',
        length: 0
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const {pageType, editData, formModel} = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.disabledNo = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        checkName: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}],
        checkNo: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}],
        regex: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}],
        length: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.checkRuleConfigId !== '') {
            const tip = '修改成功！'
            const url = '/checkRuleConfig/update'
            this.post(url, this.formModel).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          } else {
            const url = '/checkRuleConfig/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
